#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//vfs_errorcodes.h"

 
FILE *fpld_dest;
int listdirectory(char dir_name[], int flag_rec, char outputfile[]){
  
   //FILE *fpld_dest;
   //printf("Entered listdir 1\n");
   char str[100];
   char newstr1[50];
   char newstr2[50]; 
   char node_label[50];  
   char abspath[100]=""; 

   int num;
   int j=0;
   int level=0;
   int count=1;
   int pos=1;
   int i; 
   int k=0;    
   
   Tree cur_tree;   
   Tree prev_tree;
   Tree sib=cur_tree;
   Tree new;
   Tree temp;
   
   

   NODE first=NULL;   
   NODE cur=first;
   NODE last;
   
   //printf("Entered listdir 2\n");
   
   // VFS_INSUFFICIENT_ARGUMENTS
	if ((strcmp(dir_name,"") == 0) || /*(flag_rec==NULL) ||*/ (strcmp(outputfile,"") == 0)){
		printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_00); // Insufficient arguments
		return 0;
	}
   
   if(flag_rec!=0 && flag_rec!=1){
   	printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_02); // Invalid flag
        return 0;  
   
   }
   
   if(dir_name[0]!='/'){
   	printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_01); // Cannot find path/dir
        return 0;    
   }
   //printf("Output file: %s\n",outputfile);
   fpld_dest = fopen(outputfile, "wt");
   
   if(fpld_dest==NULL){
   	printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_04); // Cannot create output file
        return 0;
   
   }
   
   if(strcmp(dir_name,"/")==0){
   	 if(flag_rec==1)
      	RecurseDisplay(root);
      	
      	
      	else{
       
      	//fprintf(fpld_dest,"%s\n",root->node_label/*,strcat(prev_tree->path,prev_tree->node_label)*/);
      	temp = root->left;
      	
      	while(temp!=NULL){
      		fprintf(fpld_dest,"%s\n",temp->node_label/*,strcat(temp->path,temp->node_label)*/);
      		temp = temp->right;      	
      	}
      	
      	}
      	
      	return 1;   
   
   }
   
   
  else{ 
	
     while(dir_name[pos]!='\0'){
         strcat(abspath,"/");	
         j=0;
         num=split(dir_name,pos);
         
         if(num>(strlen(dir_name)-1)) num = strlen(dir_name)-1;
         
         for(i=pos; i<=num; i++){
            newstr2[j]=dir_name[i];
            j++;
         }
         newstr2[j]='\0';    	 
                 
         first=insert_rear_ll(newstr2,abspath,first);
         strcat(abspath,newstr2);
         if(num>=strlen(dir_name)-1) break;
         pos=num+2;
        
      }

      last=cur;
      cur=first;
      while(cur->next!=NULL){
        count=count+1;
        cur=cur->next;
      } 
      
     
      
      cur=first;
      cur_tree=root->left;
      sib=root->left;
      prev_tree=root;    
      
      while(count>0){
	  if(cur_tree!=NULL){ 
	  	//printf("%s\n",cur_tree->node_label);
        	if(strcmp(cur_tree->node_label,cur->entry) == 0){  
            		cur=cur->next;
            		prev_tree=cur_tree;
			cur_tree=cur_tree->left;
			sib=cur_tree;
			count=count-1;			
			
        	}
        	else{   
			prev_tree=cur_tree;
            		cur_tree=cur_tree->right;
        	}
	  }
	  else{   
		printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_01); // Cannot find path/dir
        	return 0;  			      
	  }
     
      } 
      
      if(flag_rec==1)
      	RecurseDisplay(prev_tree->left);
      	
      	
      else{
       
      	fprintf(fpld_dest,"%s\n",prev_tree->node_label/*,strcat(prev_tree->path,prev_tree->node_label)*/);
      	temp = prev_tree->left;
      	
      	while(temp!=NULL){
      		fprintf(fpld_dest,"%s\n",temp->node_label/*,strcat(temp->path,temp->node_label)*/);
      		temp = temp->right;      	
      	}
      	     
      }      
      
      return 1;
      
    }
}

void RecurseDisplay(Tree root)
{	
	
	if(root!=NULL)
	{        
	    fprintf(fpld_dest,"%s\n",root->node_label);				
		RecurseDisplay(root->right);		
		RecurseDisplay(root->left);
		
	}
	
}

